package cn.libinal.scloud.base.oauth.security;

import cn.libinal.scloud.base.common.dto.UserDto;
import cn.libinal.scloud.base.common.web.ResultJson;
import cn.libinal.scloud.base.oauth.security.token.CustomUserDetails;
import cn.libinal.scloud.base.oauth.service.BaseClients;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * scloud
 *
 * @author lbb created by 2019/1/22 16:04
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final BaseClients baseClients;

    public UserDetailsServiceImpl(BaseClients baseClients) {
        this.baseClients = baseClients;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("\n\n执行 loadUserByUsername 方法......\n\n");
        ResultJson<UserDto> userResult = baseClients.findByUsername(username);
        if (userResult.getStatus().equals(ResultJson.ResultEnum.ERROR.getCode())) {
            throw new UsernameNotFoundException("用户:" + username + "，不存在");
        }
        UserDto userDto = userResult.getData();
        return new CustomUserDetails(userDto);
    }
}
